package moni.anyou.com.view.view;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import org.json.JSONArray;
import org.json.JSONObject;
import org.kymjs.aframe.http.KJHttp;
import org.kymjs.aframe.http.KJStringParams;
import org.kymjs.aframe.http.StringCallBack;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import moni.anyou.com.view.R;
import moni.anyou.com.view.base.BaseActivity;
import moni.anyou.com.view.bean.InitTripDataBean;
import moni.anyou.com.view.config.SysConfig;
import moni.anyou.com.view.tool.AppTools;
import moni.anyou.com.view.tool.PermissionTools;
import moni.anyou.com.view.tool.ToastTools;
import moni.anyou.com.view.tool.Tools;
import moni.anyou.com.view.tool.UploadUtil;
import moni.anyou.com.view.utils.BaseAppAdapter;
import moni.anyou.com.view.view.my.invitefamily.InviteFriendActivity;
import moni.anyou.com.view.view.photo.PhotoDialog;
import moni.anyou.com.view.widget.NetProgressWindowDialog;
import moni.anyou.com.view.widget.dialog.MessgeDialog;
import moni.anyou.com.view.widget.pikerview.TimeSelector;
import moni.anyou.com.view.widget.pikerview.view.RelationSeletor;
import moni.anyou.com.view.widget.utils.imageload.ImageLoadUtil;

public class TeamDetailActivity extends BaseActivity implements View.OnClickListener {
    private NetProgressWindowDialog window;
    private ImageView ivUpload;
    private TextView tvScheduling;
    private TextView tvActivityFee;
    private TextView tvRemark;
    private TextView tvContact;
    private TextView tvEquipment;
    private TextView tvDisclaimer;
    private TextView tvStartTime;
    private TextView tvEndTime;
    private TextView tvRouteName;
    private TextView etTeamName;
    private GridView gvTeam;
    private RelativeLayout rlRouteName;
    private RelativeLayout rlStartTime;
    private RelativeLayout rlEndTime;
    private TextView tvActivityStrength;
    private RelativeLayout rlScheduling;
    private RelativeLayout rlActivityFee;
    private RelativeLayout rlDisclaimer;
    private RelativeLayout rlEquipment;
    private RelativeLayout rlActivityStrength;
    private RelativeLayout rlContact;
    private RelativeLayout rlRemark;
    private RelativeLayout rlUpload;
    private LinearLayout llUpload;

    private String routeID = "";

    private String mType;
    private String mVaule;
    private String mTypeName;
    private String upLoadfile;
    private String teamPic = "";  //封面图
    private BaseAppAdapter<JSONObject> userAdapter;
    private JSONArray teamMemberList = new JSONArray();
    private JSONArray teamInfo = new JSONArray();
    private String teamid = "0";
    private int status = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_detail);
        initState();
        initWindow();
        teamid =  getIntent().getStringExtra("teamID");
        init();
        LogOut.getInstance().addActivity(this);
    }

    @Override
    public void initView() {
        super.initView();
        initTitle();

        window = new NetProgressWindowDialog(mBaseActivity);
        tvTitle.setText("组团详情");
        gvTeam =  (GridView) findViewById(R.id.gv_team);
        etTeamName =  (TextView) findViewById(R.id.et_teamName);
        rlScheduling = (RelativeLayout) findViewById(R.id.rl_scheduling);
        rlActivityFee = (RelativeLayout) findViewById(R.id.rl_activityFee);
        rlContact = (RelativeLayout) findViewById(R.id.rl_contact);
        rlRouteName = (RelativeLayout) findViewById(R.id.rl_routeName);
        rlEquipment = (RelativeLayout) findViewById(R.id.rl_equipment);
        rlDisclaimer = (RelativeLayout) findViewById(R.id.rl_disclaimer);
        rlActivityStrength = (RelativeLayout) findViewById(R.id.rl_activityStrength);
        rlRemark = (RelativeLayout) findViewById(R.id.rl_remark);
        llUpload = (LinearLayout) findViewById(R.id.ll_upload);
        rlUpload = (RelativeLayout) findViewById(R.id.rl_upload);
        ivUpload = (ImageView) findViewById(R.id.iv_upload);
        tvScheduling = (TextView) findViewById(R.id.tv_scheduling);
        tvActivityFee = (TextView) findViewById(R.id.tv_activityFee);
        tvContact = (TextView) findViewById(R.id.tv_contact);
        tvEquipment = (TextView) findViewById(R.id.tv_equipment);
        tvDisclaimer = (TextView) findViewById(R.id.tv_disclaimer);
        tvActivityStrength = (TextView) findViewById(R.id.tv_activityStrength);
        tvRemark = (TextView) findViewById(R.id.tv_remark);
        tvRouteName = (TextView) findViewById(R.id.tv_routeName);
        rlStartTime = (RelativeLayout)findViewById(R.id.rl_startTime);
        rlEndTime = (RelativeLayout)findViewById(R.id.rl_endTime);
        tvStartTime = (TextView) findViewById(R.id.tv_startTime);
        tvEndTime = (TextView)findViewById(R.id.tv_endTime);
        /*
        try{
            for(int i=0;i<strInput.length;i++) {
                map.put(strInput[i],"");
            }

            setBitmaptoImageView11(SysConfig.PicUrl + SysConfig.userInfoJson.get("icon").toString(),ivUpload);
            tvRemark.setText(SysConfig.userInfoJson.get("companyname").toString());
            map.put("nick",tvNick.getText().toString());
            map.put("smallpic",SysConfig.userInfoJson.get("icon").toString());
            map.put("company",tvRemark.getText().toString());
        }
        catch(Exception ex){

        }
        */
    }

    @Override
    public void setData() {
        super.setData();
        getInitInfo();
    }

    @Override
    public void setAction() {
        super.setAction();
        ivBack.setOnClickListener(this);

    }
    private void initTeamMember(){

        ArrayList datas = new ArrayList();
        for(int i=0;i<teamMemberList.length();i++){
            try {
                datas.add(teamMemberList.get(i));
            }
            catch(Exception ex){

            }
        }
        try {
            JSONObject json = new JSONObject();
            json.put("teamMemberID", "0");
            json.put("userID", "0");
            json.put("addTime", "0");
            json.put("status", "0");
            json.put("teamID", "0");
            json.put("icon", "");
            json.put("nick", "0");
            json.put("isLeader", "999");
            json.put("routeName", "0");
            datas.add(json);
        }
        catch(Exception ex){

        }

        userAdapter = new BaseAppAdapter<JSONObject>(gvTeam, R.layout.gradview_route_team_item, datas) {

            @Override
            public View getViewIndex(JSONObject user, int position, View convertView, ViewGroup parent) {
                CircleImageView civ_head = (CircleImageView) convertView.findViewById(R.id.civ_head);
                TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                View tv_msg = (TextView) convertView.findViewById(R.id.tv_msg);
                try {
                    if (user.getString("icon") != null && !user.getString("icon").equals("")) {
                        ImageLoadUtil.getInstance(
                                getBaseContext()).
                                mImageLoader.displayImage(SysConfig.PicUrl + user.getString("icon"),
                                civ_head,
                                ImageLoadUtil.getOptions(R.drawable.loading_11,
                                        R.drawable.loading_err_11,
                                        R.drawable.loading_null_11));
                    } else {
                        civ_head.setImageResource(R.drawable.w_1);
                    }
                    tv_name.setText(user.getString("nick"));
                    if ("1".equals(user.getString("isLeader"))) {
                        tv_msg.setVisibility(View.VISIBLE);
                    } else {
                        tv_msg.setVisibility(View.GONE);
                    }
                    //1是普通成员 2是领队   999是邀请
                    switch (user.getInt("isLeader")) {
                        case 0:
                            tv_msg.setVisibility(View.GONE);
                            break;
                        case 1:
                            tv_msg.setVisibility(View.VISIBLE);
                            break;
                        case 999:
                            civ_head.setImageResource(R.drawable.jiaren);
                            tv_msg.setVisibility(View.GONE);
                            tv_name.setText("邀请");
                            break;

                    }
                    civ_head.setTag(user);
                    civ_head.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                JSONObject user = (JSONObject) v.getTag();
                                if (user.getString("isLeader").equals("999")) {
                                    Intent i = new Intent();
                                    i.putExtra("teamID", user.getString("teamID"));
                                    i.setClass(mBaseActivity, InviteFriendActivity.class);
                                    startActivity(i);
                                }
                            }
                            catch(Exception ex){

                            }

                        }
                    });
                }
                catch(Exception ex){
                    Toast.makeText(mContext,""+ ex.toString(), Toast.LENGTH_LONG).show();
                }
                return convertView;
        }
        };


    }
    private void initTeamInfo(){
       if (teamInfo.length() > 0){
           try {
               JSONObject json = (JSONObject) teamInfo.get(0);
               tvScheduling.setText(json.getString("scheduling"));
               tvActivityFee.setText(json.getString("activityFee"));
               tvContact.setText(json.getString("contact"));
               tvEquipment.setText(json.getString("equipment"));
               tvDisclaimer.setText(json.getString("disclaimer"));
               tvActivityStrength.setText(json.getString("activityStrength"));
               tvRemark.setText(json.getString("remark"));;
               tvRouteName.setText(json.getString("routeName"));
               etTeamName.setText(json.getString("teamName"));
               tvStartTime.setText(json.getString("startTime"));
               tvEndTime.setText(json.getString("endTime"));
               status = json.getInt("status");
               if (status == 1){
                   gvTeam.setVisibility(View.VISIBLE);
               }
               else{
                   gvTeam.setVisibility(View.GONE);
               }
           }
           catch(Exception ex){

           }
       }
    }



    //手机高版本样式
    private void initState() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

    }

    private SystemBarTintManager tintManager;
    //手机高版本样式
    @TargetApi(19)
    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintColor(Color.parseColor("#FEC600"));

            tintManager.setStatusBarTintEnabled(true);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            try {
                Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView");
                Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor");
                field.setAccessible(true);
                field.setInt(getWindow().getDecorView(),
                        Color.TRANSPARENT);
            } catch (Exception e) {
            }
        }

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_left:
                onBack();
                break;

            default:
                break;
        }

    }



    //得到初始化组团信息
    public void getInitInfo() {
        window.ShowWindow();
        KJHttp kjh = new KJHttp();
        KJStringParams params = new KJStringParams();
        String cmdPara = "{\"cmd\":\"32\",\"uid\":\""+ SysConfig.uid + "\",\"token\":\""+ SysConfig.token+ "\",\"teamid\":\""+ teamid+"\"}";
        params.put("sendMsg", cmdPara);

        kjh.urlGet(SysConfig.ServerUrl, params, new StringCallBack() {
            @Override
            public void onSuccess(String t) {
                try {
                    JSONObject jsonObject = new JSONObject(t);
                    int result = Integer.parseInt(jsonObject.getString("result"));
                    if (result >= 1) {
                        teamInfo = ((JSONObject)jsonObject.getJSONArray("list").get(0)).getJSONArray("teamInfo");
                        teamMemberList  = ((JSONObject)jsonObject.getJSONArray("list").get(0)).getJSONArray("teamMemberList");
                       // Toast.makeText(mContext, teamMemberList.toString(), Toast.LENGTH_LONG).show();
                        initTeamInfo();
                        initTeamMember();
                    } else {
                        if (result == -2) {
                            AppTools.jumptoLogin(mBaseActivity, result);
                        }
                        Toast.makeText(mContext, jsonObject.get("retmsg").toString(), Toast.LENGTH_LONG).show();
                    }
                } catch (Exception ex) {
                    Toast.makeText(mContext, "数据请求失败", Toast.LENGTH_LONG).show();

                }
                window.closeWindow();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(mContext, "网络异常，请稍后再试", Toast.LENGTH_LONG).show();

                window.closeWindow();
            }
        });
    }







}


